package controller;

import po.College;
import po.Grade;
import service.GradeService;
import service.serviceImpl.GradeServiceImpl;
import util.ServletRepeat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author 润
 */
@WebServlet(name = "GradeServlet",urlPatterns = "/GradeServlet")
public class GradeServlet  extends BaseServlet  {
    private GradeService service=new GradeServiceImpl();
    public void queryAllGrade(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String c = request.getParameter("cid");
        int cid = Integer.parseInt(c);
        List<Grade> grades = service.queryAllGrade(cid);
        if(grades.size()>0){
            request.setAttribute("collegeId",cid);
            request.setAttribute("grades",grades);
            request.getRequestDispatcher("/allGrades.jsp").forward(request,response);
        }else{
            request.setAttribute("collegeId",cid);
            request.setAttribute("img","暂时无相关年级！");
            request.getRequestDispatcher("/emptyGrade.jsp").forward(request,response);
        }
    }

    public void addCourse(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String c = request.getParameter("cid");
        int cid = Integer.parseInt(c);
        Grade grade = new Grade();
        grade.setName(name);
        grade.setCid(cid);
        service.insert(grade);
        boolean result1=true;
        String json1 = ServletRepeat.getJson(result1);
        response.getWriter().write(json1);
    }
    public void queryOneGrade(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String c = request.getParameter("cid");
        int cid = Integer.parseInt(c);
        Grade grade = service.queryOne(cid);
        request.setAttribute("grade",grade);
        request.setAttribute("cid",cid);
        request.getRequestDispatcher("/updateGrade.jsp").forward(request,response);

    }
    public void updateGrade(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String c = request.getParameter("cid");
        int cid = Integer.parseInt(c);
        String i = request.getParameter("id");
        int id = Integer.parseInt(i);
        String name = request.getParameter("name");
        Grade grade = new Grade();
        grade.setName(name);
        grade.setCid(cid);
        grade.setId(id);
        service.update(grade);
        boolean result2=true;
        String json2 = ServletRepeat.getJson2(result2);
        response.getWriter().write(json2);
    }
    public void deleteGrade(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String i = request.getParameter("id");
        int id = Integer.parseInt(i);
        Grade grade = service.queryOne(id);
        Integer cid = grade.getCid();
        request.setAttribute("cid",cid);
        service.delete(grade);
        List<Grade> grades = service.queryAllGrade(cid);
        if(grades.size()>0){
            request.setAttribute("collegeId",cid);
            request.setAttribute("grades",grades);
            request.getRequestDispatcher("/allGrades.jsp").forward(request,response);
        }else{
            request.setAttribute("collegeId",cid);
            request.setAttribute("img","暂时无相关年级！");
            request.getRequestDispatcher("/emptyGrade.jsp").forward(request,response);
        }

    }

    }



